package genalgoritm.count;

import genalgoritm.mediators.ValuesMediator;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.math.genetics.Chromosome;
import org.apache.commons.math.genetics.ElitisticListPopulation;
import org.apache.commons.math.genetics.FixedGenerationCount;
import org.apache.commons.math.genetics.GeneticAlgorithm;
import org.apache.commons.math.genetics.Population;
import org.apache.commons.math.genetics.RandomKeyMutation;
import org.apache.commons.math.genetics.StoppingCondition;
import org.apache.commons.math.genetics.TournamentSelection;

public class Count {
	private static final int TOURNAMENT_ARITY = 2;
	private static final int NUM_GENERATIONS = 4;
	private static final double ELITISM_RATE = 0.8;
	private ArrayList<Population> results = new ArrayList<Population>();
	
	private Population getInitialPopulation() {
		List<Chromosome> firstPop = new ArrayList<Chromosome>();
		for (Double x : ValuesMediator.getPoints()){
			firstPop.add(new RealChromosome(x));
		}
		return new ElitisticListPopulation(firstPop,Constants.limit,ELITISM_RATE);
	}

	public void count(){
		// initialize a new genetic algorithm
		GeneticAlgorithm ga = new GeneticAlgorithm(
		    ValuesMediator.getCStrategy(ValuesMediator.getCParams()),
		    ValuesMediator.getCrossover(),
		    ValuesMediator.getMStrategy(ValuesMediator.getMParams()),
		    ValuesMediator.getMutation(),
		    ValuesMediator.getSStrategy(ValuesMediator.getSParams())
		);
		        
		// initial population
		Population initial = getInitialPopulation();
		        
		// stopping condition
		StoppingCondition stopCond = ValuesMediator.getEStrategy(ValuesMediator.getEParams());
		    
		Population current = initial;
	    while (!stopCond.isSatisfied(current)) {
	    	results.add(current);
	    	current = ga.nextGeneration(current);
	    }
	}

	public ArrayList<Population> getResults() {
		return results;
	}
}
